<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring容器深度解析：BeanFactory vs ApplicationContext</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .feature-card {
            transition: all 0.3s ease;
            border-radius: 12px;
            overflow: hidden;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .diff-item {
            position: relative;
            padding-left: 2rem;
        }
        .diff-item:before {
            content: '';
            position: absolute;
            left: 0;
            top: 0.6rem;
            width: 1rem;
            height: 1rem;
            border-radius: 50%;
            background: #4f46e5;
        }
        .diagram-container {
            background: #f8fafc;
            border-radius: 12px;
            padding: 2rem;
        }
        footer {
            background: #1a202c;
        }
        a:hover {
            color: #818cf8;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-6xl font-bold mb-6">Spring容器核心解析</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深度剖析BeanFactory与ApplicationContext的设计哲学与实现差异</p>
            <div class="flex justify-center space-x-4">
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#Spring框架</span>
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#IOC容器</span>
                <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">#设计模式</span>
            </div>
        </div>
    </section>

    <!-- Intro Section -->
    <section class="py-16 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="text-center mb-12">
                <h2 class="text-3xl font-bold mb-4">容器双雄：Spring生态的核心支柱</h2>
                <p class="text-lg text-gray-600 max-w-3xl mx-auto">在Spring框架中，BeanFactory和ApplicationContext是IoC容器的两大核心接口，它们既是父子关系，又各具特色，共同构成了Spring生态的基石。</p>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-16">
                <div class="feature-card bg-white p-8 shadow-md">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                            <i class="fas fa-box-open text-indigo-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold">BeanFactory</h3>
                    </div>
                    <p class="text-gray-600 mb-4">作为基础容器接口，提供最核心的Bean管理能力，采用延迟加载策略，适用于资源受限环境。</p>
                    <ul class="space-y-2">
                        <li class="flex items-center">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i>
                            <span>轻量级实现</span>
                        </li>
                        <li class="flex items-center">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i>
                            <span>按需加载</span>
                        </li>
                        <li class="flex items-center">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i>
                            <span>基础DI支持</span>
                        </li>
                    </ul>
                </div>

                <div class="feature-card bg-white p-8 shadow-md">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                            <i class="fas fa-layer-group text-purple-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold">ApplicationContext</h3>
                    </div>
                    <p class="text-gray-600 mb-4">作为企业级容器，扩展了BeanFactory功能，提供完整的企业应用支持，推荐在大多数场景使用。</p>
                    <ul class="space-y-2">
                        <li class="flex items-center">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i>
                            <span>预加载策略</span>
                        </li>
                        <li class="flex items-center">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i>
                            <span>企业级特性</span>
                        </li>
                        <li class="flex items-center">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i>
                            <span>自动装配</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </section>

    <!-- Comparison Section -->
    <section class="py-16 px-4 bg-gray-50">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold text-center mb-12">核心差异对比</h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="grid grid-cols-1 md:grid-cols-12">
                    <!-- Header -->
                    <div class="md:col-span-3 bg-indigo-600 text-white p-6 flex items-center">
                        <h3 class="text-xl font-bold">对比维度</h3>
                    </div>
                    <div class="md:col-span-4 bg-purple-600 text-white p-6 flex items-center">
                        <h3 class="text-xl font-bold">BeanFactory</h3>
                    </div>
                    <div class="md:col-span-5 bg-indigo-800 text-white p-6 flex items-center">
                        <h3 class="text-xl font-bold">ApplicationContext</h3>
                    </div>
                    
                    <!-- Rows -->
                    <div class="md:col-span-3 border-b border-gray-200 p-6 flex items-center bg-gray-50">
                        <span class="font-medium">功能特性</span>
                    </div>
                    <div class="md:col-span-4 border-b border-gray-200 p-6">
                        <p>基本Bean管理功能，包括实例化、依赖注入、生命周期管理</p>
                    </div>
                    <div class="md:col-span-5 border-b border-gray-200 p-6">
                        <p>扩展功能：国际化支持、事件传播、AOP、资源访问等企业级特性</p>
                    </div>
                    
                    <div class="md:col-span-3 border-b border-gray-200 p-6 flex items-center bg-gray-50">
                        <span class="font-medium">实例化策略</span>
                    </div>
                    <div class="md:col-span-4 border-b border-gray-200 p-6">
                        <p>延迟加载，只有请求获取Bean时才进行实例化和初始化</p>
                    </div>
                    <div class="md:col-span-5 border-b border-gray-200 p-6">
                        <p>预实例化，容器启动时就实例化所有单例Bean并进行初始化</p>
                    </div>
                    
                    <div class="md:col-span-3 border-b border-gray-200 p-6 flex items-center bg-gray-50">
                        <span class="font-medium">配置加载</span>
                    </div>
                    <div class="md:col-span-4 border-b border-gray-200 p-6">
                        <p>需要手动指定配置文件位置或注解扫描包路径</p>
                    </div>
                    <div class="md:col-span-5 border-b border-gray-200 p-6">
                        <p>自动扫描加载所有符合条件的Bean定义，支持多种配置方式</p>
                    </div>
                    
                    <div class="md:col-span-3 border-b border-gray-200 p-6 flex items-center bg-gray-50">
                        <span class="font-medium">资源管理</span>
                    </div>
                    <div class="md:col-span-4 border-b border-gray-200 p-6">
                        <p>依赖JVM垃圾回收机制管理Bean生命周期</p>
                    </div>
                    <div class="md:col-span-5 border-b border-gray-200 p-6">
                        <p>容器关闭时自动销毁单例Bean，主动释放资源</p>
                    </div>
                    
                    <div class="md:col-span-3 p-6 flex items-center bg-gray-50">
                        <span class="font-medium">扩展接口</span>
                    </div>
                    <div class="md:col-span-4 p-6">
                        <p>仅提供基本Bean管理接口</p>
                    </div>
                    <div class="md:col-span-5 p-6">
                        <p>提供MessageSource、ApplicationEventPublisher等多种扩展接口</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Class Diagrams -->
    <section class="py-16 px-4">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold text-center mb-12">类结构关系图</h2>
            
            <div class="diagram-container mb-12">
                <div class="mermaid">
                    classDiagram
                        class BeanFactory {
                            <<interface>>
                            +getBean(name): Object
                            +containsBean(name): boolean
                            +isSingleton(name): boolean
                            +getType(name): Class
                        }
                        
                        class ListableBeanFactory {
                            <<interface>>
                            +getBeanDefinitionCount(): int
                            +getBeanNamesForType(type): String[]
                        }
                        
                        class HierarchicalBeanFactory {
                            <<interface>>
                            +getParentBeanFactory(): BeanFactory
                            +containsLocalBean(name): boolean
                        }
                        
                        class ApplicationContext {
                            <<interface>>
                            +getId(): String
                            +getApplicationName(): String
                            +getStartupDate(): long
                            +publishEvent(event): void
                        }
                        
                        BeanFactory <|-- ListableBeanFactory
                        BeanFactory <|-- HierarchicalBeanFactory
                        ListableBeanFactory <|-- ApplicationContext
                        HierarchicalBeanFactory <|-- ApplicationContext
                </div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <h3 class="text-xl font-bold mb-4">BeanFactory继承体系</h3>
                    <img src="https://cdn.nlark.com/yuque/0/2021/png/21449790/1635990383829-538bc241-e8d2-4f3b-94e8-b526b8685281.png" alt="BeanFactory继承体系" class="w-full rounded-lg shadow-md">
                </div>
                <div>
                    <h3 class="text-xl font-bold mb-4">ApplicationContext继承体系</h3>
                    <img src="https://cdn.nlark.com/yuque/0/2021/png/21449790/1635990417366-3174ee50-9851-4c26-a081-12d935d92184.png" alt="ApplicationContext继承体系" class="w-full rounded-lg shadow-md">
                </div>
            </div>
        </div>
    </section>

    <!-- Best Practices -->
    <section class="py-16 px-4 bg-indigo-50">
        <div class="container mx-auto max-w-5xl">
            <h2 class="text-3xl font-bold text-center mb-12">实践建议</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-8 rounded-lg shadow-md">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center mr-4">
                            <i class="fas fa-lightbulb text-indigo-600"></i>
                        </div>
                        <h3 class="text-xl font-bold">何时选择BeanFactory</h3>
                    </div>
                    <ul class="space-y-3">
                        <li class="diff-item">资源受限环境，需要最小化内存占用</li>
                        <li class="diff-item">需要完全控制Bean的加载时机</li>
                        <li class="diff-item">不需要企业级特性如AOP、事件等</li>
                        <li class="diff-item">自定义容器实现的场景</li>
                    </ul>
                </div>
                
                <div class="bg-white p-8 rounded-lg shadow-md">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                            <i class="fas fa-rocket text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-bold">何时选择ApplicationContext</h3>
                    </div>
                    <ul class="space-y-3">
                        <li class="diff-item">大多数企业应用场景</li>
                        <li class="diff-item">需要自动装配和注解支持</li>
                        <li class="diff-item">需要使用AOP、事件等企业特性</li>
                        <li class="diff-item">希望启动时完成所有初始化</li>
                    </ul>
                </div>
            </div>
            
            <div class="mt-12 bg-white p-8 rounded-lg shadow-md">
                <div class="flex items-center mb-4">
                    <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-4">
                        <i class="fas fa-star text-green-600"></i>
                    </div>
                    <h3 class="text-xl font-bold">关键结论</h3>
                </div>
                <p class="text-gray-700 mb-4">ApplicationContext是BeanFactory的超集，提供了更多的功能和特性，因此在实际应用中更为常用。但如果对资源要求较低或需要自定义BeanFactory的实现，也可以使用BeanFactory来管理Bean。</p>
                <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4">
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <i class="fas fa-exclamation-circle text-yellow-500 mt-1"></i>
                        </div>
                        <div class="ml-3">
                            <p class="text-sm text-yellow-700">
                                现代Spring应用开发中，通常直接使用ApplicationContext的各种实现(如AnnotationConfigApplicationContext)，只有在特殊场景下才会考虑直接使用BeanFactory。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="py-12 px-4 text-white">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold mb-2">技术小馆</h3>
                    <p class="text-gray-300">探索技术本质，分享架构真知</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors">
                        <i class="fas fa-link mr-2"></i> www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-700 mt-8 pt-8 text-center text-gray-400 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>